package org.springblade.modules.platform.algorithm;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.platform.algorithm.config.DataConfig;
import org.springblade.modules.platform.algorithm.method.BaseStationMethod;
import org.springblade.modules.platform.algorithm.method.BaseStationMethod2;
import org.springblade.modules.platform.algorithm.method.BaseStationMethod3;
import org.springblade.modules.platform.dto.DeviceRuningStateDTO;
import org.springblade.modules.platform.utils.TimeUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
@AllArgsConstructor
@Slf4j
public class CalculationMethod {


	private final BaseStationMethod baseStation;
	private final BaseStationMethod2 baseStation2;
	private final BaseStationMethod3 baseStation3;
	private static final Double NL = 156965.0;
	private static final Double ZL = 181350.0;

	private final BladeRedis bladeRedis;
	/**
	 * 去9号值，两边给9号扫描
	 *
	 */



	public void getDeviceRuningState(List<DeviceRuningStateDTO> deviceRuningStateDTOs, Integer gatherEvent) {
		try {
			List<DeviceRuningStateDTO> collect = deviceRuningStateDTOs.stream().map(this::removeOutliers).filter(Objects::nonNull).collect(Collectors.toList());
			List<DeviceRuningStateDTO> deviceRuningStateDTOS = sl2(collect);

			deviceRuningStateDTOS.forEach(x->{
				String s = TimeUtils.timeStampConversionTime(x.getAcquisitionTime(), "yyyy/MM/dd");
				if(DataConfig.map_9.containsKey(s)){
					Double x9 = DataConfig.map_9.get(s);
					if(s.equals("2024/02/03")){
						baseStation3.installDeviceRuningState(x, x.getGatherEvent(), 1, 0.0F);
						baseStation3.installDeviceRuningState(x, x.getGatherEvent(), 2, 0.0F);
					}else {
						canThe9thOnBothSides3(x,x9,1);
						canThe9thOnBothSides3(x,x9,2);
					}




				}
			});

		}catch (Exception e){
			bladeRedis.del("ACTIVE_QUERY");
		}



	}
	public void canThe9thOnBothSides3(DeviceRuningStateDTO plumbDeviceStateDTO,Double x9,Integer measurChainDirection) {
		try {

			Double v2 = baseStation.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), 1, 0.0, measurChainDirection);
			if (measurChainDirection == 1) {
				double zGame = (x9 - v2) / ZL / 4.85E-6;
				Double v1 = baseStation.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), 1, zGame, measurChainDirection);
				if (v1 > x9) {//实际值比默认值小
					Integer x = 0;
					Double y = 1.0;
					Double vx = 0.0;
					while (true) {
						zGame = zGame + y;
						vx = baseStation.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), 1, zGame, measurChainDirection);
						if (x9 > vx) {///实际值比默认值大
							for (Double i = zGame - 1; i < zGame + 1; i = i + 0.1F) {
								baseStation2.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), i, measurChainDirection);
							}
							baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), measurChainDirection, null);
							return;
						}
					}

				}else if (v1 < x9) {//实际值为负数 且大于测量值
					Integer x = 0;
					Double y = 1.0;
					Double vx1 = 0.0;
					while (true) {
						zGame = zGame - y;
						Double vx = baseStation.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), 1, zGame, measurChainDirection);
						if (x9 < vx) {///实际值比默认值大
							for (Double i = zGame - 1; i < zGame + 1; i = i + 0.1F) {
								baseStation2.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), i, measurChainDirection);
							}
							baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), measurChainDirection, null);
							return;
						}
					}

				}else {

					baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection, (float) zGame);
					return;

				}

			} else {
				double zGame = (v2 - x9  ) / NL / 4.85E-6;
				Double v1 = baseStation.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), 1, zGame, measurChainDirection);
				if (v1 > x9) {//实际值比默认值小
					Integer x = 0;
					Double y = 1.0;
					Double vx = 0.0;
					while (true) {
						zGame = zGame - y;
						vx = baseStation.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), 1, zGame, measurChainDirection);
						if (x9 > vx) {///实际值比默认值大
							for (Double i = zGame - 1; i < zGame + 1; i = i + 0.1F) {
								baseStation2.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), i, measurChainDirection);
							}
							baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), measurChainDirection, null);
							return;
						}
					}

				}else if (v1 < x9) {//实际值为负数 且大于测量值
					Integer x = 0;
					Double y = 1.0;
					Double vx = 0.0;
					while (true) {
						zGame = zGame + y;
						vx = baseStation.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), 1, zGame, measurChainDirection);
						if (x9 < vx) {///实际值比默认值大
							for (Double i = zGame - 1; i < zGame + 1; i = i + 0.1F) {
								baseStation2.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), i, measurChainDirection);
							}
							baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), measurChainDirection, null);
							return;
						}
					}

				}else {

					baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection, (float) zGame);
					return;

				}
			}

		}catch (Exception e){
			bladeRedis.del("ACTIVE_QUERY");
		}


	}
/*	public void canThe9thOnBothSides(DeviceRuningStateDTO plumbDeviceStateDTO,Double x9,Integer measurChainDirection){
		Double zGame = null;
		Object o = bladeRedis.get("device:game:" + measurChainDirection);//多少角秒
		if(o!=null){
			zGame = Double.parseDouble(o.toString());
		}
		Double v2 = baseStation.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), 1, zGame, measurChainDirection);
		if(x9>v2){//实际值比默认值大
			if(measurChainDirection==1){
				Integer x = 0;
				Float y = 1.0F;
				while(true){
					zGame = zGame - y;
					Double vx =  baseStation.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), 1, zGame ,measurChainDirection);
					if(x9<vx){
						for(Double i = zGame-1; i < zGame+1; i = i + 0.1F){
							baseStation2.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,i,measurChainDirection);
						}
						baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection,null);
						return;
					}else if(x9>vx){
						if(x>10){
							y = -1.0F;
							for(Float i = zGame-1; i < zGame+1; i = i + 0.1F){
								baseStation2.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,i,measurChainDirection);
							}
							baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection,null);
							return;
						}

					}else {
						if(x>10){
							if(zGame==null){
								zGame = 0.0F;
							}
							baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection,zGame);
						}

					}
					x++;
				}
			}else {
				Integer x = 0;
				Float y = 1.0F;
				while(true){
					zGame = zGame - y;
					Double vx =  baseStation.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), 1, zGame ,measurChainDirection);
					if(x9<vx){
						for(Float i = zGame-1; i < zGame+1; i = i + 0.1F){
							baseStation2.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,i,measurChainDirection);
						}
						baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection,null);
						return;
					}else if(x9>vx){
						y = -1.0F;
						if(x>20){
							for(Float i = zGame-1; i < zGame+1; i = i + 0.2F){
								baseStation2.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,i,measurChainDirection);
							}
							baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection,null);
							return;
						}

					}else {
						if(x>10){
							if(zGame==null){
								zGame = 0.0F;
							}
							baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection,zGame);
						}

					}
					x++;
				}
			}

		}else if(x9<v2){//实际值比默认值小
			if(measurChainDirection==1){
				Integer x = 0;
				Float y = 1.0F;
				while(true){
					zGame = zGame - y;
					Double vx =  baseStation.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), 1, zGame ,measurChainDirection);
					if(x9>vx){
						for(Float i = zGame-1; i < zGame+1; i = i + 0.1F){
							baseStation2.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,i,measurChainDirection);
						}
						baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection,null);
						return;
					}else if(x9<vx){
						y = 1.0F;
						if(x>10){
							for(Float i = zGame-1; i < zGame+1; i = i + 0.1F){
								baseStation2.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,i,measurChainDirection);
							}
							baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection,null);
							return;
						}

					}else {
						if(x>10){
							if(zGame==null){
								zGame = 0.0F;
							}
							baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection,zGame);
						}
					}
					x++;
				}
			}else {
				Integer x = 0;
				Float y = 1.0F;
				while(true){
					zGame = zGame - y;
					Double vx =  baseStation.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent(), 1, zGame ,measurChainDirection);
					if(x9>vx){
						for(Float i = zGame-1; i < zGame+1; i = i + 0.1F){
							baseStation2.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,i,measurChainDirection);
						}
						baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection,null);
						return;
					}else if(x9<vx){
						y = 0.5F;
						if(x>10){
							for(Float i = zGame-1; i < zGame+1; i = i + 0.1F){
								baseStation2.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,i,measurChainDirection);
							}
							baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection,null);
							return;
						}

					}else {
						if(x>5){
							if(zGame==null){
								zGame = 0.0F;
							}
							baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection,zGame);
						}

					}
					x++;
				}
			}

		}else {
			if(zGame==null){
				zGame = 0.0F;
			}
			baseStation3.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,measurChainDirection,zGame);
			return;

		}
	*//*	for(Float i = zGame-1; i < zGame+1; i = i + 0.1F){
			baseStation2.installDeviceRuningState(plumbDeviceStateDTO, plumbDeviceStateDTO.getGatherEvent() ,zGame,measurChainDirection);
		}*//*


	}*/

	/**
	 * 去掉异常点
	 * @param deviceRuningStateDTO
	 * @return
	 */
	public List<DeviceRuningStateDTO> sl2(List<DeviceRuningStateDTO> deviceRuningStateDTO){

		return 	deviceRuningStateDTO.stream().filter(x-> TimeUtils.getDateTimeOfTimestamp(x.getAcquisitionTime()).getHour()==4
				&&TimeUtils.getDateTimeOfTimestamp(x.getAcquisitionTime()).getMinute()<32).collect(Collectors.toList());
	}
	public DeviceRuningStateDTO removeOutliers(DeviceRuningStateDTO deviceRuningStateDTO){

		try {
/*        	Long acquisitionTime = deviceRuningStateDTO.getAcquisitionTime();//采集时间
			Object o = bladeRedis.get("acquisitionTime:");
			if(o==null){
				bladeRedis.set("acquisitionTime:",acquisitionTime);
			}else {
				if((acquisitionTime-Long.parseLong(o.toString())>(1000*60*60*2))){
					bladeRedis.set("acquisitionTime:",acquisitionTime);
					return null;
				}
			}
			bladeRedis.set("acquisitionTime:",acquisitionTime);*/
			for (DeviceRuningStateDTO.DeviceListDTO x : deviceRuningStateDTO.getDeviceList()) {
				List<DeviceRuningStateDTO.DeviceListDTO.AttributeListDTO> attributeListDTO = x.getAttributeListDTO();
				if(attributeListDTO.stream().map(DeviceRuningStateDTO.DeviceListDTO.AttributeListDTO::getKey).collect(Collectors.toList()).contains("real_value")){
					continue;

				}
	/*		DeviceRuningStateDTO.DeviceListDTO.AttributeListDTO ccd2AfterDipY = attributeListDTO.stream().filter(l -> l.getKey().equalsIgnoreCase("ccd2_after_dip_y")).findFirst().get();
			DeviceRuningStateDTO.DeviceListDTO.AttributeListDTO ccd2FrontDipY = attributeListDTO.stream().filter(l -> l.getKey().equalsIgnoreCase("ccd2_front_dip_y")).findFirst().get();

			if((ccd2AfterDipY.getValue()>7000||(ccd2AfterDipY.getValue()<=500&&ccd2AfterDipY.getValue()>0))&&
					(ccd2FrontDipY.getValue()>7000||(ccd2FrontDipY.getValue()<=500&&ccd2FrontDipY.getValue()>0))
			){
				DeviceTable one = iDeviceTableService.lambdaQuery()
						.eq(DeviceTable::getDeviceChannelId, x.getDeviceId())
						.eq(DeviceTable::getIsDeleted, 0).one();
				MeasuringPlatformType one1 = iMeasuringPlatformTypeService.getOne(Wrappers.<MeasuringPlatformType>lambdaQuery().like(MeasuringPlatformType::getDeviceId, one.getDeviceId()));
				NoticeTable noticeTable = new NoticeTable();
				noticeTable.setState(2);
				noticeTable.setEquipmentName(one1.getMeasuringPlatformName());
				noticeTable.setEquipmentId( x.getDeviceId());
				noticeTable.setGatherTime(deviceRuningStateDTO.getAcquisitionTime());
				noticeTable.setAlarmContent("CCD异常:"+ccd2AfterDipY.getKey()+":"+ccd2AfterDipY.getValue()+"||"+ccd2FrontDipY.getKey()+":"+ccd2FrontDipY.getValue());
				iNoticeTableService.save(noticeTable);
				return null;
			}
			Integer ccd1_x_after_backward = attributeListDTO.stream().filter(l -> l.getKey().equalsIgnoreCase("ccd1_x_after_backward")).findFirst().get().getValue();
			Integer ccd1_x_front_backward = attributeListDTO.stream().filter(l -> l.getKey().equalsIgnoreCase("ccd1_x_front_backward")).findFirst().get().getValue();
			Integer ccd4_x_after_backward = attributeListDTO.stream().filter(l -> l.getKey().equalsIgnoreCase("ccd4_x_after_backward")).findFirst().get().getValue();
			Integer ccd4_x_front_backward = attributeListDTO.stream().filter(l -> l.getKey().equalsIgnoreCase("ccd4_x_front_backward")).findFirst().get().getValue();

			if(!((ccd1_x_after_backward<7000&&ccd1_x_front_backward<7000&&ccd4_x_after_backward<7000&&ccd4_x_front_backward<7000)
					||(ccd1_x_front_backward>7000&&ccd4_x_front_backward>7000&&ccd1_x_after_backward<7000&&ccd4_x_after_backward<7000)
					||(ccd1_x_front_backward<7000&&ccd4_x_front_backward<7000&&ccd1_x_after_backward>7000&&ccd4_x_after_backward>7000)
			)){
				DeviceTable one = iDeviceTableService.lambdaQuery()
						.eq(DeviceTable::getDeviceChannelId, x.getDeviceId())
						.eq(DeviceTable::getIsDeleted, 0).one();
				MeasuringPlatformType one1 = iMeasuringPlatformTypeService.getOne(Wrappers.<MeasuringPlatformType>lambdaQuery().like(MeasuringPlatformType::getDeviceId, one.getDeviceId()));
				NoticeTable noticeTable = new NoticeTable();
				noticeTable.setState(2);
				noticeTable.setEquipmentName(one1.getMeasuringPlatformName());
				noticeTable.setEquipmentId( x.getDeviceId());
				noticeTable.setGatherTime(deviceRuningStateDTO.getAcquisitionTime());
				noticeTable.setAlarmContent("CCD异常:"+ccd2AfterDipY.getKey()+":"+ccd2AfterDipY.getValue()+"||"+ccd2FrontDipY.getKey()+":"+ccd2FrontDipY.getValue());
				iNoticeTableService.save(noticeTable);
				return null;
			}*/
/*			for (DeviceRuningStateDTO.DeviceListDTO.AttributeListDTO y : attributeListDTO) {

				if(!y.getKey().equalsIgnoreCase("electricity")&&!y.getKey().equalsIgnoreCase("temperature")){
					if(y.getValue()>7000||(y.getValue()<=500&&y.getValue()>0)){
						DeviceTable one = iDeviceTableService.lambdaQuery()
							.eq(DeviceTable::getDeviceChannelId, x.getDeviceId())
							.eq(DeviceTable::getIsDeleted, 0).one();
						NoticeTable noticeTable = new NoticeTable();
						noticeTable.setState(2);
						noticeTable.setEquipmentName(one.getDeviceName());
						noticeTable.setEquipmentId( x.getDeviceId());
						noticeTable.setGatherTime(deviceRuningStateDTO.getAcquisitionTime());
						noticeTable.setAlarmContent("CCD异常:"+y.getKey()+":"+y.getValue());
						iNoticeTableService.save(noticeTable);
						return null;
					}
				}

			}*/
			}


			return deviceRuningStateDTO;


		}catch (Exception e){
			bladeRedis.del("ACTIVE_QUERY");
		}
		return null;



	}

}
